const Koa = require('koa')
const fs = require('fs')
const app = new Koa()
const cors = require('koa2-cors')
const Router = require('koa-router')
const router = new Router()
const bodyParser = require('koa-bodyparser')
var robot = require('robotjs')
// Speed up the mouse.
robot.setMouseDelay(2)

var twoPI = Math.PI * 2.0
var screenSize = robot.getScreenSize()
var height = (screenSize.height / 2) - 10
var width = screenSize.width
var y = 0

// for (var x = 0; x < width; x++) {
// 	y = height * Math.sin((twoPI * x) / width) + height
// 	robot.moveMouse(x, y)
// }
// // Type "Hello World".
// robot.typeString('Hello World')

// // Press enter.
// robot.keyTap('enter')
// // Get mouse position.
// var mouse = robot.getMousePos()

// // Get pixel color in hex format.
// var hex = robot.getPixelColor(mouse.x, mouse.y)
// console.log('#' + hex + ' at x:' + mouse.x + ' y:' + mouse.y)

app.use(cors({
    credentials: true,
    origin: function (ctx) { // 设置允许来自指定域名请求
            return '*' // 允许来自所有域名请求
    },
  exposeHeaders: ['WWW-Authenticate', 'Server-Authorization', 'Date'],
  maxAge: 100,
  allowMethods: ['GET', 'POST', 'OPTIONS'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept', 'X-Custom-Header', 'anonymous', 'access-token']
}))
app.use(bodyParser())
router.get('/remoteControl', async function (ctx, next) {
    const urlstrlast = ctx.request.query.action
    switch(urlstrlast) {
      case 'speace':
        // 空格键
        robot.keyTap('space')
         break;
      case 'lightup':
       // 空格键
       robot.keyTap('lights_mon_up')
        break;
      case 'lightdown':
       // 空格键
       robot.keyTap('lights_mon_down')
        break;
      case 'jinduup':
       // 空格键
       robot.keyTap('right')
        break;
      case 'jindudown':
       // 空格键
       robot.keyTap('left')
        break;
      case 'voiceup':
       // 空格键
       robot.keyTap('audio_vol_up')
        break;
      case 'voicedown':
       // 空格键
       robot.keyTap('audio_vol_down')
        break;
      default:
 } 
        ctx.body = {
            code: 200,
            message: '成功'
        }
  })
app.use(router.routes())
let str = ''
str = str + '.__     __    __                   /\\      /\\ .__                       .__  .__                    __       ________ _______  _______  ____      /\\ ' + '\r\n'
str = str + '|  |___/  |__/  |_______   /\\     / /     / / |  |   ____   ____ _____  |  | |  |__   ____  _______/  |_  /\\ \\_____  \\\\   _  \\ \\   _  \\/_   |    / / ' + '\r\n'
str = str + '|  |  \\   __\\   __\\____ \\  \\/    / /     / /  |  |  /  _ \\_/ ___\\\\__  \\ |  | |  |  \\ /  _ \\/  ___/\\   __\\ \\/   _(__  </  /_\\  \\/  /_\\  \\|   |   / /  ' + '\r\n'
str = str + '|   Y  \\  |  |  | |  |_> > /\\   / /     / /   |  |_(  <_> )  \\___ / __ \\|  |_|   Y  (  <_> )___ \\  |  |   /\\  /       \\  \\_/   \\  \\_/   \\   |  / /   ' + '\r\n'
str = str + '|___|  /__|  |__| |   __/  \\/  / /     / /    |____/\\____/ \\___  >____  /____/___|  /\\____/____  > |__|   \\/ /______  /\\_____  /\\_____  /___| / /    ' + '\r\n'
str = str + '     \\/           |__|         \\/      \\/                      \\/     \\/          \\/           \\/                   \\/       \\/       \\/      \\/     ' + '\r\n'
console.log(str)
console.log('生成器后台已启动   http://localhost:3001/ ')
app.listen(3001)
